﻿using System;
using System.Text.RegularExpressions;
using UPos.Core.Entity;
using UPos.Core.LogProvider;

namespace UPos.Core
{
    public class Services
    {
        private static Regex _isNumber = new Regex("^-?\\d+$|^(-?\\d+)(\\.\\d+)?$");

        public static bool IsNumber(string inputData)
        {
            Match m = _isNumber.Match(inputData);
            return m.Success;
        }

        public bool CallWebService(Sale sale, ShopInfo info, out string message)
        {
            ttpos.salesSoapClient MyTTPos = new ttpos.salesSoapClient();

            ttpos.postsalescreaterequest posReq = new ttpos.postsalescreaterequest();
            // 请求头
            ttpos.requestheader posHdr = new ttpos.requestheader();
            posHdr.licensekey = info.Licensekey;
            posHdr.username = info.Username;
            posHdr.password = info.Password;
            posHdr.messagetype = "SALESDATA";
            posHdr.messageid = "332";
            posHdr.version = "V332M";
            // 销售单头
            ttpos.saleshdr salesHdr = new ttpos.saleshdr();
            salesHdr.issuedate_yyyymmdd = //创建日期
            salesHdr.txdate_yyyymmdd = sale.BizDate.ToString("yyyyMMdd"); // 交易日期
            salesHdr.issuetime_hhmmss = //创建时间
            salesHdr.txtime_hhmmss = sale.BizDate.ToString("HHmmss"); // 交易时间

            salesHdr.mallid = info.Mallid;
            salesHdr.storecode = info.Storecode;

            salesHdr.tillid = "01"; // sale.MachineNo.ToString(); //收银机号 
            salesHdr.salestype = sale.Amount > 0 ? "SA" : "SR"; //单据类型
            salesHdr.txdocno = sale.BillNumber; //销售单号
            salesHdr.cashier = sale.CreaterNumber; //收银员编号
            salesHdr.netqty = sale.Amount; //净数量
            salesHdr.sellingamount = sale.Paymentmoney; //销售金额
            salesHdr.netamount = sale.Paymentmoney; //净金额
            salesHdr.paidamount = sale.Paymentmoney; //付款金额
            salesHdr.issueby = sale.CreaterName; //创建人
            salesHdr.mallitemcode = info.Itemcode;

            ttpos.ArrayOfSalesitem arrSalesItem = new ttpos.ArrayOfSalesitem();
            ttpos.ArrayOfSalestender arrSalesTender = new ttpos.ArrayOfSalestender();

            ttpos.salesitem salesItem = null;

            for (int i = 0; i < sale.SaleItems.Count; i++)
            {
                salesItem = new ttpos.salesitem();

                salesItem.iscounteritemcode = "1";
                salesItem.lineno = i + 1;
                salesItem.storecode = salesHdr.storecode;

                salesItem.mallitemcode = // 货号
                salesItem.counteritemcode = // 租户货号
                salesItem.itemcode = // 商品编号
                salesItem.plucode = info.Itemcode; 

                salesItem.qty = sale.SaleItems[i].Qty; // 数量
                salesItem.netamount = sale.SaleItems[i].TotalPrice; // 净金额

                arrSalesItem.Add(salesItem);
            }

            ttpos.salestender salesTender;

            for (int i = 0; i < sale.ReceiveItems.Count; i++)
            {
                salesTender = new ttpos.salestender();
                salesTender.lineno = i + 1;

                salesTender.tendercode = sale.ReceiveItems[i].PaymentCode; // 付款代码
                salesTender.payamount = // 付款金额
                salesTender.baseamount = sale.ReceiveItems[i].PayMoney; // 本位币金额

                arrSalesTender.Add(salesTender);
            }

            ttpos.salesdelivery salesDelivery = new ttpos.salesdelivery();

            posReq.header = posHdr;
            posReq.salestotal = salesHdr;
            posReq.salesitems = arrSalesItem;
            posReq.salestenders = arrSalesTender;
            posReq.salesdlvy = salesDelivery;

            ttpos.postsalescreateresponse posRsp = MyTTPos.postsalescreate(posReq);

            bool result = posRsp.header.responsecode.ToString() == "0";
            message = result ? "生成交易成功"
                : posRsp.header.responsemessage + "(" + posRsp.header.responsecode.ToString() + ")";

            LogSQLite.Write("CallWebService", "item({0}) result({1}) mess({2}) reservedocno({3})", sale.FID, result, message, posRsp.salestotal.reservedocno);
            return result;
        }

        void CallDailySalesSummary(Settlement settlement, out string message)
        {
            ttpos.salesSoapClient MyTTPos = new ttpos.salesSoapClient();

            ttpos.postdailysalessummaryrequest sumReq = new ttpos.postdailysalessummaryrequest();

            // 请求头
            ttpos.requestheader posHdr = new ttpos.requestheader();
            posHdr.licensekey = "";
            posHdr.username = "";
            posHdr.password = "";
            posHdr.messagetype = "SALESDATA";
            posHdr.messageid = "332";
            posHdr.version = "V332M";

            ttpos.dailysalessummary posDailySum = new ttpos.dailysalessummary();

            posDailySum.txdate_yyyymmdd = DateTime.Now.ToString("yyyyMMdd"); // 日结日期
            posDailySum.txtime_hhmmss = "223000"; // 日结时间
            posDailySum.mallid = "";
            posDailySum.storecode = "";
            posDailySum.tillid = settlement.MachineNo;
            posDailySum.txdocno = "PDS" + DateTime.Now.ToString("yyyyMMdd") + "223000";
            posDailySum.ttlsalesqty = settlement.Amount;
            posDailySum.ttlsalesamt = settlement.TotalPrice;
            //posDailySum.ttlrefundqty = 0;  //退货总数量
            //posDailySum.ttlrefundamt = 0;  //退货总金额
            posDailySum.ttldoccount = settlement.SaleCount;   //总销售笔数
            posDailySum.cashier = settlement.Cashier; // 收银员编号
            posDailySum.issuedate_yyyymmdd = DateTime.Now.ToString("yyyyMMdd"); // 创建日期

            sumReq.header = posHdr;
            sumReq.salessummary = posDailySum;

            ttpos.postdailysalessummaryresponse sumRsp = MyTTPos.postdailysalessummary(sumReq);

            if (sumRsp.header.responsecode.ToString() == "0")
            {
                message = "生成日结成功";
            }
            else
            {
                message = sumRsp.header.responsemessage + "(" + sumRsp.header.responsecode.ToString() + ")";
            }
        }
    }
}
